import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { UiService } from '../../ui.service';
import { GB } from '../../../environments/environment';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { I18nService } from '../../core/i18n/i18n.service';
import { ActionSheetConfig, SkinType, ActionSheetComponent, ActionSheetService, RatingConfig, PickerService, DatePickerComponent } from 'ngx-weui';
import { MessageService } from './../../core/message.service';
import { InfiniteLoaderComponent } from 'ngx-weui/infiniteloader';
import { ApiService } from 'src/app/core/api';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { StorageService } from 'src/app/core/sotrage/storage.service';

@Component({
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less'],
  encapsulation: ViewEncapsulation.None,
})
export class OrderComponent implements OnInit {

  @ViewChild('datePicker', { static: true }) datePicker: DatePickerComponent;
  date: any;
  del: any;
  user: any;
  now = new Date();
  DT: any = {
    // min: new Date(this.now.getFullYear() + '-' + this.now.getMonth() + '-' + this.now.getDate()),
    min: new Date(),
    max: new Date()
  };
  checkboxData: any[] = ['A', 'B'];
  travelProductId: string;
  travel: any;//旅行社
  orderForm: FormGroup;
  selGzData=[];
  selData: any =[];
  isGZ=false;

  constructor(
    private api: ApiService,
    private sto: StorageService,
    private route: ActivatedRoute,
    private router: Router,
    private fb: FormBuilder,
    private srv: PickerService,

  ) {
    // this.message.sendMessage('index');
  }
  isLXS;
  ngOnInit() {
    this.travel = this.route.snapshot.queryParams;
    this.isLXS = this.travel['isLXS'];
    this.ajxGetUser();
    let date = new Date();
    let today = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    this.orderForm = this.fb.group({
      email: ['', [Validators.required, Validators.pattern('^[a-zA-Z0-9_\-]{2,}@[a-zA-Z0-9_\-]{2,}(\.[a-zA-Z0-9_\-]+){1,2}$')]],
      contactWay: ['', []],
      loginId: ['', []],
      mobile: ['', [Validators.required]],
      reserveDate: ['', [Validators.required]],
      peopleNum: [1, [Validators.required]],
      guideName: ['', []],
      memo: ['', []],
      travel: ['', []],
      guideId: ['', []],
    });
  };

  lys: any;
  travelAgencyId:string;
  ajxGetUser() {
    this.api.post('用户详情', {}).then((e: any) => {
      this.user = e.data;
      this.orderForm.patchValue(e.data);
    });

    let data = {};
    let url;
    this.travelProductId = this.route.snapshot.queryParams['travelGuideId'];
    this.travelAgencyId = this.route.snapshot.queryParams['travelAgencyId'];
    if (this.route.snapshot.queryParams['type'] == 1) {
      url = '导游详情';
      data['travelGuideId'] = this.travelProductId;
    } else {
      url = '导游产品详情';
      data['travelProductId'] = this.travelProductId;
    }
    switch (this.isLXS) {
      case 'true':
        this.isLXS = true;
        this.travelProductId = this.route.snapshot.queryParams['travelProductId'];
        this.api.get('旅游社详情', { travelProductId: this.route.snapshot.queryParams['travelProductId'] }).then((e: any) => {
          this.orderForm.patchValue({ travel: e['travelAgencyForUserView']['name'] });
          this.selData =  e['agencyTravelGuideList'];
          this.selData.push({ id: '', name: '由旅行社指定',isFollowed:'Y' })
          for (let i = 0; i < this.selData.length; i++) {
            const t = this.selData[i];
            t.isFollowed == 'Y' ? this.selGzData.push(t) : '';
          }
        });
        break;
      case 'false':
        this.isLXS = false;
        this.api.get(url, data).then((e: any) => {
          this.orderForm.patchValue({ guideName: e['travelGuideForUserView']['name'] });
          this.orderForm.patchValue({ guideId: e['travelGuideForUserView']['id'] });
        });
        break;
      case 'gzAgency':
          this.api.get('通过旅行社编号调用旅游社详情', { travelAgencyId:this.travelAgencyId }).then((e: any) => {
            this.orderForm.patchValue({ travel: e['travelAgencyForUserView']['name'] });
            this.selData =  e['agencyTravelGuideList'];
            this.selData.push({ id: '', name: '由旅行社指定',isFollowed:'Y' })
            for (let i = 0; i < this.selData.length; i++) {
              const t = this.selData[i];
              t.isFollowed == 'Y' ? this.selGzData.push(t) : '';
            }
          });
        break;
    
      default:
        break;
    }
    if (this.isLXS != 'false') {
    } else {
    }
  }
 
  onChangeOnlyGZ() {
    this.isGZ = !this.isGZ;
  }
  onshow(e) {
    console.log(e)

  }
  srvRes: any = '';
  onShowBySrv(type: string) {
    this.srv.showDateTime('date').subscribe((res: any) => {
      this.srvRes = res.value;
    });

  }


  sub() {
    for (const v in this.orderForm.controls) {
      this.orderForm.controls[v].markAsDirty();
      this.orderForm.controls[v].updateValueAndValidity();
    }
    let data = this.orderForm.value;
    data['agencyId'] = this.travelAgencyId ? this.travelAgencyId : '' ;
    data['travelProductId'] = this.route.snapshot.queryParams['travelProductId'] ? this.route.snapshot.queryParams['travelProductId'] : '' ;

    if(this.orderForm.valid){
        this.api.post('创建旅游订单', data).then((e: any) => {
          this.sto.set('pay-data', e.data);
          this.router.navigate(['/recharge'], {
            queryParams: {
              travelOrderId: e.data['travelOrderId']
            }
          });
    
        });
    }
  }

}
